<?php
session_start();
include_once("./configuration.php");
include_once("./db_functions.php");
if(isset($_POST['passwd']) && isset($_POST['user'])) {
  if(db_check_password($_POST['user'], $_POST['passwd'])) {
    $_SESSION['user'] = $_POST['user'];
    $_SESSION['logged'] = true;
    $userdir = "users/".$_SESSION['user'];
    $_SESSION['uploads'] =  dirname(__FILE__)."/users/".$_POST['user']."/uploads/";
    $_SESSION['processed'] =  dirname(__FILE__)."/users/".$_POST['user']."/processed/";
    $_SESSION['homedir'] =  dirname(__FILE__)."/users/".$_POST['user']."/";
    $_SESSION['completed'] =  $_SESSION['homedir']."completed/";
    $_SESSION['filterlist'] = dirname(__FILE__)."/users/".$_POST['user']."/filterlist/";
    $_SESSION['filterlist_file'] = "filterlist.txt";
    $_SESSION['language'] = "default"; 					// Default value..
    if(!is_dir($_SESSION['homedir']))
      	mkdir($_SESSION['homedir']);
    if(!is_dir($_SESSION['uploads']))
      	mkdir($_SESSION['uploads']);
    if(!is_dir($_SESSION['processed']))
      	mkdir($_SESSION['processed']);
    if(!is_dir($_SESSION['completed']))
      	mkdir($_SESSION['completed']);
    if(!is_dir($_SESSION['filterlist'])){
      	mkdir($_SESSION['filterlist']);
      	$filePath = $_SESSION['filterlist'].$_SESSION['filterlist_file'];
		$fileHandle = fopen($filePath, 'w') or die("can't open file");
		fwrite($fileHandle, 'FooBar');
		fclose($fileHandle);
    }

    header("Location: ./index.php?p=file_management");
  } else {
    header("Location: ./index.php?p=info&loginfail");
  }
} else {
  header("Location: ./index.php");
}
?>
